import grails.converters.JSON
class PlanAjaxController {

    def index = { }
    def validTeacherCourseTime = {        
        log.info("planCount="+params.planCount)
        log.info("oldTeacherCount="+params.oldTeacherCount)
        log.info("nowTeacherCount="+params.nowTeacherCount)
        Plan p = Plan.get(params.planID)
        Teacher t = Teacher.get(params.teacherID)
        log.info(params)
        def resultsCount = Plan.countByTeacher(t)

        def countPlan = params.oldTeacherCount?params.oldTeacherCount.toInteger()+params.nowTeacherCount.toInteger():params.nowTeacherCount.toInteger()

        if(resultsCount+countPlan > params.planCount.toInteger()){
            return render(['success':false,'message' : '此教师的课时已经安排满了，请重新安排时间！'] as JSON) 
        }else{
            return render(['success':true,'message' : '可用'] as JSON) 
        }
        
//        if(Teacher.countByUsernameAndSchool(params.name,School.get(params.school)) > 0){
//            return render(['success':false,'message' : '此登录名已被使用！'] as JSON) 
//        }else{
//            return render(['success':true,'message' : '此登录名可用！'] as JSON) 
//        }        
    }

    def validResetTeacherCourseTime = {        
        Plan p = Plan.get(params.planID)
        Teacher t = Teacher.get(params.teacherID)
        log.info(params)
        def results = Plan.withCriteria {
                eq ('week',p.week)
                eq ('teacher',t)
                eq ('restTime',p.restTime)
                ne ('id',p.id)
        }
        log.info("results.size("+results.size())
        if(results.size() > 0){
            return render(['success':false,'message' : '此教师在这个时间已经有安排上课了！请重新选择老师或安排科目'] as JSON) 
        }else{
            return render(['success':true,'message' : '可用'] as JSON) 
        }
         
    }
}
